namespace FIREFLYX {
    /**
     * buffer 编码 转换为utf8 字符串
     * @param bytes buffer
     * @returns 
     */
    export function decodeUtf8(bytes: Uint8Array) {
        let encoded = "";
        for (let i = 0, len = bytes.length; i < len; i++) {
            let code16 = bytes[i].toString(16);
            encoded += "%" + (code16.length == 2 ? code16 : "0" + code16);
        }
        return decodeURIComponent(encoded);
    }

    const CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
    const uuidTempArr = new Array(36);

    /** 获取一个UUID */
    export function makeUUID() {
        let chars = CHARS, uuid = uuidTempArr, rnd = 0, r;
        for (let i = 0; i < 36; i++) {
            if (i == 8 || i == 13 || i == 18 || i == 23) {
                uuid[i] = "-";
            } else if (i == 14) {
                uuid[i] = "4";
            } else {
                if (rnd <= 0x02) rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
                r = rnd & 0xf;
                rnd = rnd >> 4;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
        return uuid.join("");
    }

    /**
         * 获取文件的名字通过资源路径
         * @param path 资源路径
         * @returns 文件的名字
         */
    export function GetFileName(path: string) {
        if (!path) return ``;
        let idx = path.indexOf(`.`);
        if (idx == -1) return ``;
        let sub = path.substring(0, idx);
        if (!sub) return ``;
        sub = sub.replace(`\\`, `/`);
        idx = sub.lastIndexOf(`/`);
        if (idx == -1) return ``;
        return sub.substring(idx + 1);
    }

    /**
     * 获取 文件 原始后缀
     * @param path 资源路径
     * @returns 原始后缀
     */
    export function GetFileRawSuffix(path: string) {
        if (!path) return ``;
        let idx = path.lastIndexOf(`.`);
        if (idx == -1) return ``;
        return path.substring(idx + 1);
    }

    /**
     * 获取 文件 目录路径
     * @param path 资源路径
     * @returns 目录路径
     */
    export function GetFileFolderPath(path: string) {
        if (!path) return ``;
        path = path.replace(`\\`, `/`);
        let idx = path.indexOf(`/`);
        if (idx == -1) return ``;
        return path.substring(0, idx);
    }
}